﻿using System;
using System.Collections.Generic;
using Ext.Net;
using eShop.Business;
using eShop.Model;
using System.Web.UI.WebControls;

public partial class myaccount_address_list : SitePublicBasePage
{
    private int _pageSize = 0;
    private int _totalItem = 0;
    private int _currentPageIndex = 0;
    private int _totalPage = 0;
    private bool _isLastPage = false;
    private bool _isFirstPage = false; 
    private bool _showAllPage = false;

    private void ReadPagingData()
    {
        this._pageSize = Convert.ToInt32(ViewState["Page_Size"]);
        this._totalItem = Convert.ToInt32(ViewState["Total_Item"]);
        this._currentPageIndex = Convert.ToInt32(ViewState["Current_Page_Index"]);
        this._totalPage = Convert.ToInt32(ViewState["Total_Page"]);
        this._isLastPage = Convert.ToBoolean(ViewState["Is_Last_Page"]);
        this._isFirstPage = Convert.ToBoolean(ViewState["Is_First_Page"]);        
        this._showAllPage = Convert.ToBoolean(ViewState["Show_All_Page"]);
    }

    private void WritePagingData()
    {
        ViewState["Page_Size"] = this._pageSize;
        ViewState["Total_Item"] = this._totalItem;
        ViewState["Current_Page_Index"] = this._currentPageIndex;
        ViewState["Total_Page"] = this._totalPage;
        ViewState["Is_Last_Page"] = this._isLastPage;
        ViewState["Is_First_Page"] = this._isFirstPage;       
        ViewState["Show_All_Page"] = this._showAllPage;
    }

    private List<Address> FetchData()
    {
        return AddressManager.GetAddressesByUserId(SessionManager.LoggedUser.Id);
    }

    private int GetTotalItemCount()
    {
        return AddressManager.GetAddressesByUserId(SessionManager.LoggedUser.Id).Count;
    }

    private void BindGrid()
    {
        try
        {
            ReadPagingData();

            List<Address> productPurchaseOrder = FetchData();

            int firstItemPosition = (this._currentPageIndex - 1) * this._pageSize;
            int lastItemPosition = this._currentPageIndex * this._pageSize - 1;

            this._totalItem = GetTotalItemCount();
            this._totalPage = (int)Math.Ceiling(this._totalItem / (double)this._pageSize);

            if (this._showAllPage || lastItemPosition >= this._totalItem)
                lastItemPosition = this._totalItem - 1;

            repAddressLists.DataSource = productPurchaseOrder;
            repAddressLists.DataBind();

            lblFirstItemNumber.Text = this._totalItem == 0 ? "0" : (firstItemPosition + 1).ToString();
            lblLastItemNumber.Text = (lastItemPosition + 1).ToString();
            lblTotalItem.Text = this._totalItem.ToString();

            BindPager();
        }
        catch (Exception ex)
        {

        }
    }

    private void LoadData()
    {
        this._pageSize = SiteUtility.GridDefaultPageSize;
        this._totalItem = GetTotalItemCount();
        this._currentPageIndex = 1;
        this._totalPage = (int)Math.Ceiling(this._totalItem / (double)this._pageSize);
        this._isLastPage = IsLastPage();
        this._isFirstPage = IsFirstPage();       
        this._showAllPage = false;
        WritePagingData();
        BindGrid();
    }

    protected void Paging_Command(object sender, CommandEventArgs e)
    {
        if (e.CommandName == "paging")
        {
            ReadPagingData();

            if (Convert.ToString(e.CommandArgument) == "All")
            {
                this._currentPageIndex = 1;
                this._showAllPage = true;
            }
            else
            {
                int currentPage = Convert.ToInt32(e.CommandArgument);
                this._currentPageIndex = currentPage;
                this._showAllPage = false;
            }
            // grvProductPurchaseOrder.EditIndex = -1;
            WritePagingData();
            BindGrid();
        }
    }
    private void BindPager()
    {
        List<string> pages = new List<string>();
        for (int i = 1; i <= this._totalPage; i++)
            pages.Add(i.ToString());

        rptPaging.DataSource = pages;
        rptPaging.DataBind();
    }

    private bool IsFirstPage()
    {
        if (this._currentPageIndex == 1)
            return true;
        else
            return false;
    }

    private bool IsLastPage()
    {
        if (this._currentPageIndex == this._totalPage)
            return true;
        else
            return false;
    }


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (!SessionManager.IsUserLoggedIn)
            {
                Response.Redirect(CustomSiteMap.Login);

            }
            else
            {
                this._currentPageIndex = 1;
                LoadData();        
            }
        }
        
    }   
  
}